//
//  UIButton+ClickBlock.m
//  LeaderScience
//
//  Created by CoderYee on 2017/2/6.
//  Copyright © 2017年 YeeCoder. All rights reserved.
//

#import "UIButton+ClickBlock.h"
#include <objc/runtime.h>

@implementation UIButton (ClickBlock)
static const void *UIControl_clickBlock = "UIControl_clickBlock";
- (void)handleControlEvent:(UIControlEvents)controlEvent withBlock:(ButtonBlock)action
{
    objc_setAssociatedObject(self, UIControl_clickBlock, action,OBJC_ASSOCIATION_COPY_NONATOMIC);
    [self removeTarget:self action:@selector(callActionBlock:) forControlEvents:controlEvent];
    if (action)
    {
      [self addTarget:self action:@selector(callActionBlock:) forControlEvents:controlEvent];
    }
    
  
}
- (void)callActionBlock:(id)sender
{
    ButtonBlock block = (ButtonBlock)objc_getAssociatedObject(self, &UIControl_clickBlock);
    if (block)
   {
        block();
   }
}
@end
